<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>新增{{ lebelWord[meetType] }}请批单</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="infoForm" label-width="120px" ref="editForm" class="form-box">
          <el-form-item :label="`${lebelWord[meetType]}用途：`" v-if="meetType!==2">
            <el-input v-model="infoForm.purpose" :placeholder="`${lebelWord[meetType]}用途`" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="`${lebelWord[meetType]}文件名：`" v-else>
            <el-input v-model="infoForm.purpose" :placeholder="`${lebelWord[meetType]}文件名`" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="`${lebelWord[meetType]}周期：`">
            <el-input v-model="infoForm.cycle" :placeholder="`${lebelWord[meetType]}周期`" size="small"></el-input>
          </el-form-item>
          <el-form-item label="展示位置" v-if="Number(meetType)==2||Number(meetType)==3">
            <el-input v-model="infoForm.position" placeholder="展示位置" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="`${lebelWord[meetType]}频次：`" v-if="Number(meetType)==1">
            <el-input v-model="infoForm.number" :placeholder="`${lebelWord[meetType]}频次`" size="small"></el-input>
          </el-form-item>
          <el-form-item label="费用金额：" v-if="Number(meetType)==1">
            <el-input v-model="infoForm.money" placeholder="费用金额" size="small"></el-input>
          </el-form-item>
          <el-form-item :label="`${lebelWord[meetType]}用途说明：`" prop="num">
            <el-input v-model="infoForm.content"
                      :placeholder="`${lebelWord[meetType]}用途说明`"
                      size="small"
                      type="textarea"
                      maxlength="300"
                      show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="集体决策：" prop="num">
            <el-radio-group v-model="infoForm.is_meeting">
              <el-radio :label="1">开启</el-radio>
              <el-radio :label="0">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="会议人员:" v-if="infoForm.is_meeting">
            <div class="user-list">
              <el-button type="primary" @click="getUserList()" size="small"><i class="el-icon-plus"></i>选择会议人员
              </el-button>
            </div>
            <div>
              <el-tag
                  :key="tag.id"
                  v-for="(tag,index) in dynamicTags"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(index)"
                  class="mr-10"
              >{{ tag.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="申请部门：">
            <el-select v-model="infoForm.department_id" placeholder="请选择申请部门" size="small">
              <el-option
                  v-for="item in departmentList" :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <div class="file-get-box">
            <el-divider>{{ `${lebelWord[meetType]}用途说明文件` }}</el-divider>
            <div v-for="(item, index) in fileList" :key="index" class="file-get-body">
              <p class="file-get-list">
                {{ index + 1 }}.
                <btn_file :option="item"></btn_file>
              </p>
              <span class="deteilSpan" @click="deteleFn(index,'fileList')">删除</span>
            </div>
            <uploadFileModle number="20" @getFile="getFile($event,'fileList')" ref="uploadFileModle" :sycnStatus="true"
                             fileType="interaction_add" :showfile="false" :fileListPush="getList"></uploadFileModle>
          </div>
          <div class="file-get-box">
            <el-divider>合同</el-divider>
            <div v-for="(item, index) in contractList" :key="index" class="file-get-body">
              <p class="file-get-list">
                {{ index + 1 }}.
                <btn_file :option="item"></btn_file>
              </p>
              <span class="deteilSpan" @click="deteleFn(index,'contractList')">删除</span>
            </div>
            <uploadFileModle number="20" @getFile="getFile($event,'contractList')" ref="uploadFileModle"
                             :sycnStatus="true"
                             fileType="interaction_add" :showfile="false"
                             :fileListPush="contractList_file"></uploadFileModle>
          </div>
        </el-form>

      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定并提交
        </el-button>
      </div>
    </div>
    <MultipleTeacherModle :visible.sync="teacherVisible" @studentFn="studentFn"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
  </div>
</template>

<script>
import {departmentIndex} from '@/api/admin/system'
import {approveAdd, approveAdd_public, approveAdd_advert} from '@/api/admin/approveApply'

export default {
  props: ['visible', 'meetType'], // recruitmentType examine 审核
  components: {},
  data() {
    return {
      teacherVisible: false,
      dialogVisible: this.visible,
      infoForm: {
        department_id: '', // 	字符串 	必填 	1 	- 	部门id
        purpose: '', // // 	字符串 	必填 	1 	- 	业务用途
        cycle: '', // // 	字符串 	必填 	1 	- 	业务周期
        number: '', // // 	字符串 	必填 	1 	- 	次数
        money: '', // 	字符串 	必填 	1 	- 	金额
        content: '', // 	字符串 	必填 	1 	- 	说明
        file: '', // 	字符串 	必填 	1 	- 	文件
        metting: '', // 	字符串 	必填 	1 	- 	会议人员
        metting_file: '', // 	字符串 	必填 	1 	- 	会议文件
        contract: '', // 	字符串 	必填 	1 	- 	合同
        is_meeting: 0, // 	字符串 	必填 	1 	- 	是否需要会议
        position: '',
      },
      getList: [],
      fileList: [], //  说明文件
      dynamicTags: [],
      dynamicTagsArray: {},
      departmentList: [],
      contractList: [], // 合同
      contractList_file: [],
      lebelWord: {
        1: '业务',
        2: '公示',
        3: '文宣'
      }
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.getDepart()
      } else {
        this.fileList = []
        this.dynamicTags = []
        this.dynamicTagsArray = {}
        this.departmentList = []
        this.contractList = [] // 合同
        this.contractList_file = []
        this.getList = []
        this.infoForm = {
          department_id: '', // 	字符串 	必填 	1 	- 	部门id
          purpose: '', // // 	字符串 	必填 	1 	- 	业务用途
          cycle: '', // // 	字符串 	必填 	1 	- 	业务周期
          number: '', // // 	字符串 	必填 	1 	- 	次数
          money: '', // 	字符串 	必填 	1 	- 	金额
          content: '', // 	字符串 	必填 	1 	- 	说明
          file: '', // 	字符串 	必填 	1 	- 	文件
          metting: '', // 	字符串 	必填 	1 	- 	会议人员
          metting_file: '', // 	字符串 	必填 	1 	- 	会议文件
          contract: '', // 	字符串 	必填 	1 	- 	合同
          is_meeting: 0, // 	字符串 	必填 	1 	- 	是否需要会议
        }
      }
    }
  },
  created() {

  },
  methods: {
    getDepart() {
      departmentIndex({p: 0}).then(res => {
        console.log('res:', res)
        this.departmentList = res.list
      })
    },
    getUserList() {
      let list = {};
      for (let item of this.dynamicTags) {
        list[item.id] = item
      }
      this.dynamicTagsArray = JSON.parse(JSON.stringify(list))
      this.teacherVisible = true
    },
    handleClose(index) {
      this.dynamicTags.splice(index, 1)
    },
    deteleFn(index, option) {
      // option 1 说明文件 2 会议记录 3 合同
      let src_ = this[option][index].src
      let list = ''
      if (option === 'fileList') {
        list = 'getList'
      } else if (option === 'contractList') {
        list = 'contractList_file'
      } else if (option === 'logList') {
        list = 'getlogList'
      }
      if (this[list].length) {
        let index_ = 0
        for (let i in this[list]) {
          let item = this[list][i]
          if (item.src === src_) {
            index_ = i
          }
        }
        this[list].splice(index_, 1)
      }
      this[option].splice(index, 1)
    },
    getFile(option, type) {
      console.log('收到附件数据：', option, type)
      let list = this.getJson(this[type])
      if (type === 'fileList') {
        this.getList = this.getJson(option)
      } else if (type === 'logList') {
        this.getlogList = this.getJson(option)
      } else if (type === 'contractList') {
        this.contractList_file = this.getJson(option)
      }
      if (option) {
        let url_ = []
        for (let item of list) {
          url_.push(item.src)
        }
        for (let item of option) {
          if (url_.indexOf(item.src) === -1) {
            this[type].push({
              name: item.name,
              src: item.src
            })
          }
        }
      }
    },
    studentFn(option) {
      console.log('option:', option)
      this.dynamicTags = []
      let list = [];
      for (let i in option) {
        list.push(option[i])
      }
      this.dynamicTags = JSON.parse(JSON.stringify(list))
    },
    checkEditFn() {
      console.log(this.infoForm)
      let data_ = this.getJson(this.infoForm)
      if (Number(data_.is_meeting) === 1) {
        if (!this.dynamicTags.length) {
          this.$message.error('选择会议人员')
          return
        }
        let a_ = []
        for (let item of this.dynamicTags) {
          a_.push(item.id)
        }
        data_.metting = a_.join(',')
      }
      data_.file = JSON.stringify(this.fileList)
      data_.contract = JSON.stringify(this.contractList)
      console.log('data_:', data_)
      // let state = 1
      // if (state) {
      //   return
      // }
      if (Number(this.meetType) === 1) {
        delete data_.position
        approveAdd(data_).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else if (Number(this.meetType) === 2) {
        delete data_.number
        delete data_.money
        approveAdd_public(data_).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else if (Number(this.meetType) === 3) {
        delete data_.number
        delete data_.money
        approveAdd_advert(data_).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },

    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 650px;
  height: 600px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 0px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}

.file-get-list {
  display: flex;
  padding: 5px;
  background: #eee;
  border-radius: 4px;
  align-items: center;
  flex: 1;
}

.file-get-body {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.deteilSpan {
  padding: 0 10px;
  font-size: 12px;
}

.form-box {
  padding: 20px 20px 40px 0;
}

.form-list-line {
  display: flex;
  align-items: center;
}

.form-list-line .w-100 {
  width: 160px !important;
}

.form-list-line .w-80 {
  width: 100% !important;
}

.form-list-line-right {
  flex: 1;
}

.file-get-box {
  padding-left: 20px;
}
</style>


